package com.fh.module_mine.viewmodel

import com.fh.lib_base.base.BaseBean
import com.fh.lib_base.base.BaseViewModel
import com.fh.lib_base.base.MyApplication
import com.fh.lib_base.data.DataRepository
import com.fh.lib_base.data.bean.UserBean
import com.fh.lib_base.extension.ApiSubscriberHelper
import com.fh.lib_base.util.RxThreadHelper


/**
 * @author Alwyn
 * @Date 2020/10/15
 * @Description
 */
class LoginViewModel(application: MyApplication, dataRepository: DataRepository) :
    BaseViewModel<DataRepository>(application, dataRepository) {

     fun loginByPwd(account: String,pwd: String) {
        if (account.isNullOrBlank() || pwd.isNullOrBlank()) {
            showNormalToast("账号或密码不能为空")
            return
        }
        model.apply {
            userLogin(account, pwd)
                .compose(RxThreadHelper.rxSchedulerHelper(this@LoginViewModel))
                .doOnSubscribe { showLoading() }
                .subscribe(object : ApiSubscriberHelper<BaseBean<UserBean>>() {
                    override fun onResult(t: BaseBean<UserBean>) {
                        dismissLoading()
                        if (t.errorCode == 0) {
                            showSuccessToast("登录成功")
                        }
                    }
                    override fun onFailed(msg: String?) {
                        dismissLoading()
                        showNormalToast(msg)
                    }
                })
        }
    }
}